#!/usr/bin/env python
#This script genereates a seaquence of random tests and compares them with the ref.

import sys
import random
import string
import subprocess

random.seed (42)
i = 0
OK=0
NOK=0

dict_path=raw_input("Chemin vers le dictionnaire (defaut ../dict.bin) : ")
if (dict_path == ''):
    dict_path="../dict.bin"

ref_bin=raw_input("Chemin vers la ref (defaut : ../../osx/TextMiningApp) : ")
if (ref_bin == ''):
    ref_bin="../../osx/TextMiningApp"

ref_dict=raw_input("Chemin vers le dictionnaire de la ref (defaut : ../../osx/dict.bin) : ")
if (ref_dict == ''):
    ref_dict="../../osx/dict.bin"

number=input("Nombre de tests : ")


while i < int(number):
    i = i + 1
    print "Test number " + str(i) + " :"
    d = random.randint (0, 4)
    word = ''.join(random.choice(string.ascii_letters + string.punctuation + string.digits) for x in range(random.randint(1, 12)))

    print "Searching " + word + " with distance " + str(d)
    input_string = "approx " + str(d) + " " + word

    p1 = subprocess.Popen (["echo", input_string], stdout=subprocess.PIPE)
    p2 = subprocess.Popen(["../TextMiningApp", dict_path], stdin=p1.stdout, stdout=subprocess.PIPE)

    myoutput = p2.communicate()[0]

    p3 = subprocess.Popen (["echo", input_string], stdout=subprocess.PIPE)
    p4 = subprocess.Popen([ref_bin, ref_dict], stdin=p3.stdout, stdout=subprocess.PIPE)

    refoutput = p4.communicate()[0]

    f = open('mytmp', 'w')
    f2 = open ('reftmp', 'w')

    f.write (myoutput)
    f2.write (refoutput)

    f.close ()
    f2.close()

    output = subprocess.call (["diff", "mytmp", "reftmp"])

    if (output == 0):
        print "Test " + str(i) + " : OK"
        OK = OK + 1
    else:
        print "Test " + str(i) + " : NOK"
        NOK = NOK + 1

print str(OK) + "/" + str(number)
